package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jay
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("orders = {}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        log.info("page = {}, pageSize = {}",page, pageSize);
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> ordersWrapper = new LambdaQueryWrapper<>();
        ordersWrapper.eq(Orders::getUserId, userId);// id过滤
        ordersWrapper.orderByDesc(Orders::getOrderTime);// 排序
        orderService.page(ordersPage, ordersWrapper);

        //老样子
        Page<OrdersDto> ordersDtoPage = new Page<>();
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");//对象拷贝
        ArrayList<OrdersDto> ordersDtos = new ArrayList<>();
        List<Orders> records = ordersPage.getRecords();
        //遍历查询 List<OrderDetail> 并赋值
        for (int i = 0; i < records.size(); i++) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(records.get(i), ordersDto);
            //根据OrderId查询 List<OrderDetail>
            LambdaQueryWrapper<OrderDetail> orderDetailWrapper = new LambdaQueryWrapper<>();
            orderDetailWrapper.eq(OrderDetail::getOrderId, records.get(i).getId());//OrderId过滤
            List<OrderDetail> orderDetails = orderDetailService.list(orderDetailWrapper);
            //赋值
            ordersDto.setOrderDetails(orderDetails);
            ordersDtos.add(ordersDto);
        }
        ordersDtoPage.setRecords(ordersDtos);
        return R.success(ordersDtoPage);
    }

    /**
     * 订单明细分页查询
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime ){
        log.info("page = {}, page = {},page = {},page = {},page = {}",
                page, pageSize, number, beginTime, endTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //下单时间排序
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //订单号过滤
        queryWrapper.like(number != null,Orders::getNumber, number);
        //下单时间过滤
        queryWrapper.between(beginTime != null,Orders::getOrderTime,beginTime,endTime);
        //分页查询 select * from orders
        //        where number like (%number%)
        //        and order_time between in (beginTime, endTime)
        //        order by order_time desc
        //        limit 10;
        orderService.page(ordersPage, queryWrapper);
        return R.success(ordersPage);
    }

    /**
     * 修改订单状态(派送、完成)
     * @return
     */
    @PutMapping
    public R<Orders> update(@RequestBody Orders orders){
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId, orders.getId());
        Orders order = orderService.getOne(queryWrapper);
        order.setStatus(orders.getStatus());
        orderService.updateById(order);
        return R.success(order);
    }
}
